package com.kanche.bigbaby.collection.ui;

import android.content.Context;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;

import com.kanche.bigbaby.collection.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.kkche.bss.utils.StringUtils;

//import com.kkche.merchant.utils.StringUtil;

/**
 * Created by nealmi on 6/11/14.
 */
public abstract class ItemConfig {
    private String key;
    private String value;
    private String displayValue;
    private String label;
    private int inputType = InputType.TYPE_CLASS_TEXT;
    private String caption = "";
    private boolean singleLine = true;
    private Map<String, Object> extra;
    private String errorMessage;

    public ItemConfig() {

    }

    protected ItemConfig(String key, String value, String label) {
        this.key = key;
        this.value = value;
        this.label = label;
    }

    public void clearErrorMessage() {
        errorMessage = null;
    }

    public abstract View getView(View convertView, Context mContext);

    public Map<String, Object> getExtra() {
        return extra;
    }

    public void setExtra(Map<String, Object> extra) {
        this.extra = extra;
    }

    public int getInputType() {
        return inputType;
    }

    public void setInputType(int inputType) {
        this.inputType = inputType;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getDisplayValue() {
        if (StringUtils.hasText(displayValue))
            return displayValue;
        else
            return "";
    }

    public void setDisplayValue(String displayValue) {
        this.displayValue = displayValue;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public boolean isSingleLine() {
        return singleLine;
    }

    public void setSingleLine(boolean singleLine) {
        this.singleLine = singleLine;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public static class Builder {
        private String key;
        private String value;
        private String displayValue;
        private String label;
        private String hint;
        private int inputType = InputType.TYPE_CLASS_TEXT;
        private String caption = "";
        private Map<String, Object> extra;
        private boolean singleLine = true;
        private boolean editable = true;
        private int width;
        private int height;
        private List<Map<String, String>> spinnerData = new ArrayList<Map<String, String>>();
        private ItemEditTextWithActionButtonConfig.OnActionListener onActionListener;
        private InputFilter[] inputFilters;
        private String actionButtonText;
        private List<Map<String, String>> radioGroupData = new ArrayList<Map<String, String>>();
        private boolean noMaxWidth;

        public Builder key(String key) {
            this.key = key;
            return this;
        }

        public Builder actionButtonText(String actionButtonText) {
            this.actionButtonText = actionButtonText;
            return this;
        }

        public Builder inputFilters(InputFilter[] inputFilters) {
            this.inputFilters = inputFilters;
            return this;
        }

        public Builder onActionListener(ItemEditTextWithActionButtonConfig.OnActionListener onActionListener) {
            this.onActionListener = onActionListener;
            return this;
        }

        public Builder value(String value) {
            this.value = value;
            return this;
        }

        public Builder displayValue(String displayValue) {
            this.displayValue = displayValue;
            return this;
        }

        public Builder label(String label) {
            this.label = label;
            return this;
        }

        public Builder caption(String caption) {
            this.caption = caption;
            return this;
        }

        public Builder inputType(int inputType) {
            this.inputType = inputType;
            return this;
        }

        public Builder extra(Map<String, Object> extra) {
            this.extra = extra;
            return this;
        }

        public Builder hint(String hint) {
            this.hint = hint;
            return this;
        }

        public Builder editable(boolean editable) {
            this.editable = editable;
            return this;
        }


        public Builder singleLine(boolean singleLine) {
            this.singleLine = singleLine;
            return this;
        }

        public Builder spinnerData(List<Map<String, String>> spinnerData) {
            this.spinnerData = spinnerData;
            return this;
        }

        public Builder radioGroupData(List<Map<String, String>> radioGroupData) {
            this.radioGroupData = radioGroupData;
            return this;
        }

        public Builder noMaxWidth(boolean noMaxWidth) {
            this.noMaxWidth = noMaxWidth;
            return this;
        }

        public Builder width(int width) {
            this.width = width;
            return this;
        }

        public Builder height(int height) {
            this.height = height;
            return this;
        }


        public ItemConfig buildDatePickerConfig() {
            ItemDatePickerConfig itemConfig = new ItemDatePickerConfig(key, value, label, caption);
            itemConfig.setHint(hint);
            configure(itemConfig);
            return itemConfig;
        }

        private void configure(ItemConfig itemConfig) {
            itemConfig.setExtra(extra);
            itemConfig.setInputType(inputType);
            itemConfig.setDisplayValue(displayValue);
            itemConfig.setSingleLine(singleLine);
        }

        public ItemConfig buildEditTextConfig() {
            ItemEditTextConfig itemConfig = new ItemEditTextConfig(key, value, label, hint, caption, editable);
            configure(itemConfig);
            itemConfig.setInputFilters(inputFilters);
            itemConfig.setEditTextWidth(noMaxWidth);
            return itemConfig;
        }

        public ItemConfig buildEditTextActionButtonConfig() {
            ItemEditTextWithActionButtonConfig itemConfig = new ItemEditTextWithActionButtonConfig(key, value, label, hint, caption, editable);
            configure(itemConfig);
            itemConfig.setOnActionListener(onActionListener);
            itemConfig.setInputFilters(inputFilters);
            itemConfig.setActionButtonText(actionButtonText);
            return itemConfig;
        }

        public ItemConfig buildSwitchConfig() {
            ItemConfig itemConfig = new ItemSwitchConfig(key, value, label, hint, caption);
            configure(itemConfig);
            return itemConfig;
        }


        public ItemConfig buildSpinnerConfig() {
            ItemConfig itemConfig = new ItemSpinnerConfig(key, value, label, spinnerData);
            configure(itemConfig);
            return itemConfig;
        }

        public ItemConfig buildRadioGroupConfig() {
            ItemConfig itemConfig = new ItemRadioGroupConfig(key, value, label, radioGroupData);
            configure(itemConfig);
            return itemConfig;
        }

        public ItemConfig buildSelectorConfig() {
            ItemConfig itemConfig = new ItemSelectorConfig(key, value, displayValue, label, caption);
            configure(itemConfig);
            return itemConfig;
        }


        public Builder addExtra(String key, String value) {
            if (extra == null) {
                extra = new HashMap<String, Object>();
            }
            extra.put(key, value);
            return this;
        }

        public ItemConfig buildAvatarConfig() {
            ItemAvatarConfig itemConfig = new ItemAvatarConfig(key, value, label, hint);
            configure(itemConfig);
            itemConfig.setWidth(width);
            itemConfig.setHeight(height);
            return itemConfig;
        }
    }
}
